package com.warmlight.voicepacket;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.warmlight.voicepacket.adapter.AllPlayerCoverAdapter;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.data.AllplayerData;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.eventbus.ShareUnlock;
import com.warmlight.voicepacket.fragment.CustomBottomSheetDialogFragment;
import com.warmlight.voicepacket.http.Callback;
import com.warmlight.voicepacket.http.HttpClient;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;

import static com.warmlight.voicepacket.http.HttpConfig.COVER;
import static com.warmlight.voicepacket.http.HttpConfig.SERVER;

public class AllPlayerAcitivity extends BaseActivity implements View.OnClickListener{
//    private FooterListView footerListView;
//    private View vHead;
//    private LinearLayout ll_header;
//    private AllPlayerAdapter adapter=null;
    private LinearLayout  ll_back;
    private TextView tv_header_titlename;
//    private ImageView iv_playimg;
//    private  boolean ISBODAN=true;//播单页面
    private  AllplayerData.DataBean.ShareBean sharedata;
//    private List<AllplayerData.DataBean.ListBean> listBean;
//    private  List<AllplayerData.DataBean.ListBean> datas=new ArrayList<>();
//    public static final int UPDATE = 0x1;
    private int page=1;
    private AllplayerData playdeatalData;
    private SlidingTabLayout tab_cover;
    private ViewPager vp_cover;
    private AllPlayerCoverAdapter allPlayerCoverAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playdeatal);
        initView();
        initData();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onBackPressed() {
        if (isTaskRoot()){
            finish();
            Intent intent = new Intent(this,SplashActivity.class);
            startActivity(intent);
        }else {
            super.onBackPressed();
        }
    }

    /**
     * 分享解锁回调
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(ShareUnlock messageEvent) {
        if (allPlayerCoverAdapter != null) {
            allPlayerCoverAdapter.refresh(vp_cover,messageEvent);
        }
    }

    private void initData() {
        final TreeMap<String, String> list=new TreeMap<>();
//        list.put("cover_id",getIntent().getStringExtra(LIST_ID));
        list.put("page",String.valueOf(page));
        list.put("pagesize","10");

        HttpClient.post(SERVER+COVER, list, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, String response) {
                Gson gson = new Gson();
//                String objst=response.body().string();
                playdeatalData = gson.fromJson(response, AllplayerData.class);
                final List<AllplayerData.DataBean.CategoryBean> category = playdeatalData.getData().getCategory();
                if (category.size()>0){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initTab(category);
                        }
                    });

                }
//                listBean= playdeatalData.getData().getList();
//                if (listBean.size()>0){
//
//
//                for (int j = 0;j<listBean.size();j++){
//                    datas.add(listBean.get(j));
//                }
                sharedata=playdeatalData.getData().getShare();
//                Message msg = new Message();
//                msg.what = UPDATE;
//                mHandler.sendMessage(msg);
//                }
            }
        });
    }

    private void initTab(List<AllplayerData.DataBean.CategoryBean> category) {
        Bundle bundle = getIntent().getExtras();
        String type = "";
        String pushtitle = "";
        if (bundle != null){
            if (bundle.containsKey("type")){
                type = bundle.getString("type");
            }
            if (bundle.containsKey("pushtitle")){
                pushtitle = bundle.getString("pushtitle");
            }
        }
        allPlayerCoverAdapter = new AllPlayerCoverAdapter(getSupportFragmentManager(),category,type,pushtitle);
        vp_cover.setAdapter(allPlayerCoverAdapter);
        tab_cover.setViewPager(vp_cover);
        if (bundle != null){
            String category_id = bundle.getString("category_id");
            if (!TextUtils.isEmpty(category_id)){
                for (int i=0;i<category.size();i++){
                    AllplayerData.DataBean.CategoryBean categoryBean = category.get(i);
                    if (categoryBean.getId().equals(category_id)){
                        vp_cover.setCurrentItem(i,false);
                        break;
                    }
                }
            }
        }
    }

//    private Handler mHandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//                case UPDATE:
//                    if (listBean.size()<20){
//                        footerListView.getFooterView().setVisibility(View.GONE);
//                    }
//                    if (adapter==null){
//                        adapter = new AllPlayerAdapter(AllPlayerAcitivity.this,datas , getSupportFragmentManager(),ISBODAN);
//                        footerListView.setAdapter(adapter);
//                    }else {
//                        adapter.notifyDataSetChanged();
//                    }
//                    break;
//            }
//
//        }
//    };

    private void initView() {
        tab_cover = (SlidingTabLayout) findViewById(R.id.tab_cover);
        vp_cover = (ViewPager) findViewById(R.id.vp_cover);

//        footerListView = (FooterListView) findViewById(R.id.flv_play);

        tv_header_titlename= (TextView) findViewById(R.id.tv_header_titlename);
        ll_back= (LinearLayout) findViewById(R.id.ll_back);
       ImageView  iv_header_right= (ImageView) findViewById(R.id.iv_header_right);
        iv_header_right.setOnClickListener(this);
        iv_header_right.setVisibility(View.VISIBLE);
        iv_header_right.setImageDrawable(getResources().getDrawable(R.drawable.icon_share_all_cover));
        ll_back.setOnClickListener(this);
        tv_header_titlename.setText("播单");

//        footerListView.setOnLastItemVisiableListener(new FooterListView.LastItemVisiableListener() {
//            @Override
//            public void onLastItemVisiable() {
//                page++;
//                initData();
//            }
//        });



    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_header_right:
                if (sharedata != null) {
                    PlaydeatalData.DataBean.ShareBean shareBean2 = new PlaydeatalData.DataBean.ShareBean();
                    shareBean2.setDesc(sharedata.getDesc());
                    shareBean2.setH5_url(sharedata.getH5_url());
                    shareBean2.setPic_url(sharedata.getPic_url());
                    shareBean2.setTitle(sharedata.getTitle());
                    new CustomBottomSheetDialogFragment(shareBean2).show(getSupportFragmentManager(), "Dialog");//展示分享弹窗
                }
                break;
        }
    }



}
